import org.springframework.beans.factory.annotation.Autowired;
import com.ynny.Entity.Courses;
import com.ynny.Entity.Scores;
import com.ynny.Entity.optional_courses;
import com.ynny.Service.ScoreService;
import com.ynny.Service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:StudentMansger.xml"})

public class StudentManagerTest {
    @Autowired
    private UserService userService;

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private CourseService courseService;

    @Test
    public void test() throws Exception {
//        test1();
        test2();
    }

    public void test1() throws Exception {
        // 教师登录
        int login = userService.login("11015", "654321");
        System.out.println("插入20220102003的学生成绩记录");
        //插入学生成绩
        if (login > 0) {
            updateScores("20220102003");
        }
//       查询所有的学生成绩
        printAllScores();


        System.out.println("更新20220102003学生的数学成绩");
//        更新指定学生的数学成绩
        int i = scoreService.updateScore("20220102003", "math", 100.0);
        if (i > 0){
            System.out.println("更新成功");
        }

        //        再次查询所有的学生成绩
        printAllScores();

    }

    public void test2() throws Exception {
        //        学生登录
        int login1 = userService.login("20220102005", "123456");
        if (login1 > 0) {
            System.out.println("查询20220102002学生的成绩记录");
            //查询该学生成绩记录
            Scores scores = scoreService.queryScoreByNumber("20220102002");
            System.out.println(scores);
        }

//        查询待选课程记录
        List<Courses> query = courseService.query();
        printCourses(query);

        System.out.println("执行选课操作");
//        执行选课操作
        int i = courseService.selectCourse("20220102005","math");
        if (i > 0){
            System.out.println("选课成功");
        }else System.out.println("选课失败");

        System.out.println("查询选课信息记录");
//        查询选课信息记录
        optional_courses optionalCourses = courseService.queryCourseByNumber("20220102003");
        System.out.println(optionalCourses);

//        查询待选课程记录
        List<Courses> query2 = courseService.query();
        printCourses(query2);
    }

    private void updateScores(String number) {
        scoreService.updateScore(number, "Chinese", 90.0);
        scoreService.updateScore(number, "math", 100.0);
        scoreService.updateScore(number, "English", 89.0);
    }

    private void printAllScores() {
        List<Scores> scores = scoreService.queryAllScore();
        if (!scores.isEmpty()) {
            System.out.println("查询所有的学生成绩");
            for (Scores score : scores) {
                System.out.println(score);
            }
        } else {
            System.out.println("为空");
        }
    }

    private void printCourses(List<Courses> courses) {
        if (courses != null && !courses.isEmpty()) {
            for (Courses course : courses) {
                System.out.println("查询待选课程记录");
                System.out.println(course);
            }
        } else {
            System.out.println("为空");
        }
    }
}
